package com.walkerma.library;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.os.Environment;

public class FileProcess {

	//tested
	public static boolean isFolderExists(String strPath){
		File dirDest = new File(strPath);
		return dirDest.exists();
	}


	//tested
	public static String createDir(String dirSrc, String dirDest){
		String strPath = dirSrc + File.separator + dirDest;
		if(isFolderExists(strPath)) return strPath;	
		try{			
			File file = new File(strPath);
			file.mkdirs();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return strPath;
	}


	//tested
	public static boolean isExternalStorageValid(){
		String strStatus = Environment.getExternalStorageState();
		return strStatus.equals(Environment.MEDIA_MOUNTED);				
	}


	//tested
	public static boolean writeText(String strPath, String strTxt){
		File file = new File(strPath); 
		try {            
			FileOutputStream fos = new FileOutputStream(file);  
			fos.write(strTxt.getBytes());  
			fos.close();
			return true;
		} catch (Exception e) {  
			// TODO Auto-generated catch block  
			e.printStackTrace();  
			return false;
		}  
	}

	
public static boolean writeInternalData(Context context, 
		String strPath, String strTxt){		
	FileOutputStream fos = null;      
	try {
		fos = context.openFileOutput(strPath, 0);
		try {
			fos.write(strTxt.getBytes());
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	} catch (FileNotFoundException e) {
		e.printStackTrace();
		return false;
	}
}


public static String readInternalData(Context context, String strPath){
	FileInputStream fis = null;
	byte[] buffer = null;
	try {
		fis = context.openFileInput(strPath);
		try {
			buffer = new byte[fis.available()];
			fis.read(buffer);
			return new String(buffer);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	} catch (FileNotFoundException e) {
		e.printStackTrace();
		return null;
	}
}
}

